function getDate (str) {
    if (str) {
        if (typeof str === 'number' || /^\d+$/.test(str)) {
            return new Date(+str);
        } else if (typeof str === 'string') {
            return new Date(str.replace(/-/g, '/'));
        } else if (str.constructor === Date) {
            return str
        }
    }
}

export default function (start, end) {
    try {
        if (arguments.length > 1) {
            end = getDate(end);
            start = getDate(start);
        } else {
            end = getDate(start);
            start = new Date();
        }
        const s1 = start.getTime(), s2 = end.getTime();
        const total = (s2 - s1) / 1000;
        //计算整数天数
        const day = parseInt(total / (24 * 60 * 60));
        //取得算出天数后剩余的秒数
        const afterDay = total - day * 24 * 60 * 60;
        //计算整数小时数
        const hour = parseInt(afterDay / (60 * 60));
        //取得算出小时数后剩余的秒数
        const afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60;
        //计算整数分
        const minute = parseInt(afterHour / 60);
        //取得算出分后剩余的秒数
        const second = Math.ceil(total - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);
        return {
            day,
            hour,
            minute,
            second,
            second_total: total
        }
    } catch (e) {
        return {}
    }
}
